#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QString IP=GetHostIp();
    ui->comboBoxtargetip->addItem(IP);

    socket=new QUdpSocket(this);//用于与连接的客户端通讯的QTcpSocket
    //本机的端口
    port=QString::number(ui->spinBoxbindport->value());

    ui->pushButton_start->setDisabled(false);
    ui->pushButton_stop->setDisabled(true);
    
    connect(socket,&QUdpSocket::readyRead,this,&MainWindow::ReceiveDate);

}

MainWindow::~MainWindow()
{
    delete ui;
}

QString  MainWindow::GetHostIp()
{
    QString str;
    QString hostname=QHostInfo::localHostName();
    QHostInfo hostIp= QHostInfo::fromName(hostname);
    QList<QHostAddress>  list=hostIp.addresses();
    if(!list.empty())
    {
        for(QHostAddress IP:list)
        {
            if(IP.protocol()==QAbstractSocket::IPv4Protocol)
            {
                str=IP.toString();
                break;
            }
        }
    }
    return str;
}

//发送消息
void MainWindow::on_pushButton_sendmsg_clicked()
{
/*qint64 writeDatagram(const QByteArray &datagram,
    const QHostAddress &host,
            quint16 port);*/

    QString str =ui->lineEditmsg->text();
    QByteArray datagram=str.toUtf8();
    QHostAddress address(ui->comboBoxtargetip->currentText());
    quint16 port=ui->spinBoxtargetport->value();

    socket->writeDatagram(datagram,address,port);

    ui->plainTextEditdispmsg->appendPlainText("[out]："+str);
    ui->lineEditmsg->clear();
    ui->lineEditmsg->setFocus();//// 将光标焦点定位到编辑框控件


}
//广播消息
void MainWindow::on_pushButton_broadcastmsg_clicked()
{

    QString str =ui->lineEditmsg->text();
    QByteArray datagram=str.toUtf8();
    quint16 port=ui->spinBoxtargetport->value();

    socket->writeDatagram(datagram,QHostAddress::Broadcast,port);

    ui->plainTextEditdispmsg->appendPlainText("广播[out]："+str);
    ui->lineEditmsg->clear();
    ui->lineEditmsg->setFocus();//// 将光标焦点定位到编辑框控件

}

void MainWindow::on_pushButton_start_clicked()
{
   quint16 port=ui->spinBoxbindport->value();

   if(socket->bind(port))
   {
       ui->plainTextEditdispmsg->appendPlainText("**********绑定成功**********");
       ui->plainTextEditdispmsg->appendPlainText("$$$$$$$$$$绑定端口$$$$$$$$$$"+QString::number(port));
       ui->pushButton_start->setDisabled(true);
       ui->pushButton_stop->setDisabled(false);
   }
   else
   {
       ui->plainTextEditdispmsg->appendPlainText("**********绑定失败**********");
       ui->pushButton_start->setDisabled(false);
       ui->pushButton_stop->setDisabled(true);
   }
}
//停止服务
void MainWindow::on_pushButton_stop_clicked()
{
    socket->abort();
    ui->plainTextEditdispmsg->appendPlainText("ui->plainTextEditdispmsg->appendPlainText");
    
}

//接收到数据
void MainWindow::ReceiveDate()
{
    while(socket->hasPendingDatagrams())
    {
        //readDatagram(char *data, qint64 maxSize, QHostAddress *address = nullptr, quint16 *port = nullptr)
        QByteArray buffer;
        buffer.resize(socket->pendingDatagramSize());
        char *data=buffer.data();
        QHostAddress hostaddress;
        quint16 port1;
        socket->readDatagram(data,buffer.size(),&hostaddress,&port1);

        QString peer=("[From::"+hostaddress.toString()+":"+QString::number(port1)+"]:");
        QString message=QString(data);
        ui->plainTextEditdispmsg->appendPlainText(peer+message);

    }
}

//关闭窗口
void MainWindow::closeEvent(QCloseEvent *event)
{
  socket->deleteLater();
  event->accept();
  
}
